package cn.deepmax.anime.web.support;

import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.PooledObjectFactory;
import org.apache.commons.pool2.impl.DefaultPooledObject;
import org.openqa.selenium.WebDriver;

public class WebDriverPoolFactory<T extends WebDriver> implements PooledObjectFactory<WebDriver> {


    private WebDriverFactory<T> factory ;

    public WebDriverPoolFactory(WebDriverFactory<T> factory) {
        this.factory = factory;
    }

    @Override
    public PooledObject<WebDriver> makeObject() throws Exception {
        WebDriver webDriver = factory.newInstance();
        return new DefaultPooledObject<>(webDriver);
    }

    @Override
    public void destroyObject(PooledObject<WebDriver> pooledObject) throws Exception {
        pooledObject.getObject()
                .close();
    }

    @Override
    public boolean validateObject(PooledObject<WebDriver> pooledObject) {
        return true;
    }

    @Override
    public void activateObject(PooledObject<WebDriver> pooledObject) throws Exception {

    }

    @Override
    public void passivateObject(PooledObject<WebDriver> pooledObject) throws Exception {

    }
}
